namespace Sharpmark.PerfectLife.Core.Picture.Utilities.ImageToText
{
    using System;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Text;

    public class StringText : BaseText
    {
        protected string _content;
        protected int _wordWidth;
        protected int _wordHeight;
        protected bool _isVertical;

        public StringText(int wordWidth, int wordHeight,
            char bgchar, char ftchar, string cn,
            Font font, bool isVertical)
            : base(new Photo(wordWidth, wordHeight), wordWidth, wordHeight)
        {
            BackgroundChar = bgchar;
            FrontChar = ftchar;
            Content = cn;
            Font = font;

            _isVertical = isVertical;

            WordWidth = wordWidth;
            WordHeight = wordHeight;
        }

        public char BackgroundChar { get; set; }
        public Font Font { get; set; }
        public char FrontChar { get; set; }
        public string Content
        {
            get { return _content; }
            set
            {
                _content = value;
                if (!_isVertical)
                {
                    _content = _content.Replace("\n", "");

                    CharsWidth = _wordWidth * _content.Length;
                }
                else
                {
                    _content = _content.Replace("\n", "");

                    StringBuilder sbcontent = new StringBuilder();
                    foreach (char element in _content.ToCharArray())
                    {
                        sbcontent.Append(element);
                        sbcontent.Append('\n');
                    }

                    _content = sbcontent.ToString(); //.TrimEnd(new char[] { '\n' });

                    CharsHeight = _wordHeight * _content.Length / 2;
                }
            }
        }
        public int WordWidth
        {
            get
            {
                return _wordWidth;
            }
            set
            {
                _wordWidth = value;
                if (!_isVertical)
                    CharsWidth = _wordWidth * _content.Length;
                else
                    CharsWidth = _wordWidth;
            }
        }
        public int WordHeight
        {
            get
            {
                return _wordHeight;
            }
            set
            {
                _wordHeight = value;
                if (_isVertical)
                    CharsHeight = _wordHeight * _content.Length / 2;
                else
                    CharsHeight = _wordHeight;
            }
        }
        public bool IsVertical
        {
            get { return _isVertical; }
            set
            {
                _isVertical = value;
                if (!_isVertical)
                {
                    _content = _content.Replace("\n", "");

                    CharsWidth = _wordWidth * _content.Length;
                    CharsHeight = _wordHeight;
                }
                else
                {
                    _content = _content.Replace("\n", "");
                    StringBuilder sbcontent = new StringBuilder();
                    foreach (char element in _content.ToCharArray())
                    {
                        //不能写成element + 'n',这样字符码相加，得到的是一个数值。
                        sbcontent.Append(element);
                        sbcontent.Append('\n');
                    }

                    _content = sbcontent.ToString(); //.TrimEnd(new char[] { '\n' });

                    CharsWidth = _wordWidth;
                    // content 含 '\n'，故 / 2
                    CharsHeight = _wordHeight * _content.Length / 2;
                }
            }
        }
        public override int CharsHeight
        {
            get { return base.CharsHeight; }
            set
            {
                base.CharsHeight = value;
                if (_isVertical)
                    _wordHeight = CharsHeight / _content.Length * 2;
            }
        }
        public override int CharsWidth
        {
            get { return base.CharsWidth; }
            set
            {
                base.CharsWidth = value;
                if (!_isVertical)
                    _wordWidth = CharsWidth / _content.Length;
            }
        }
        public override string GenerateText()
        {
            CreateStringBitmap();

            return base.GenerateText();
        }
        private void CreateStringBitmap()
        {
            //TODO: ASCII和汉字表示不同，长宽比要调节
            //TODO: 字体放大缩小的时候还是会有一些冗余编码在,可以慢慢改.
            PixelFormat format = PixelFormat.Format32bppArgb;

            Font = new System.Drawing.Font(Font.Name,
                (_wordWidth > _wordHeight) ? _wordWidth : _wordHeight,
                Font.Style);

            // 获取字体大小
            SizeF size = Graphics.FromImage(
                new Bitmap(CharsWidth, CharsHeight, format)).MeasureString(Content, Font);

            // 根据字体创建图形
            int sw = Int32.Parse(Math.Round(size.Width).ToString());
            int sh = Int32.Parse(Math.Round(size.Height).ToString());
            Bitmap bitmapOrg = new Bitmap(sw, sh, format);
            Graphics g = Graphics.FromImage(bitmapOrg);

            // 描绘图形
            g.FillRectangle(Brushes.White, 0, 0, sw, sh);
            // g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;
            g.DrawString(Content, Font, Brushes.Black, 0, 0);

            // 缩放到指定大小
            Photo = new Photo((Bitmap)bitmapOrg);
        }

        protected override string ConvertPixelToString(Color color)
        {
            if (color.ToArgb() == Color.White.ToArgb())
                return BackgroundChar.ToString();
            else
                return FrontChar.ToString();
        }
    }
}